/*
 * @Author: VintonHuang
 * @Date: 2021-07-01 09:35:12
 * @LastEditTime: 2021-09-07 17:35:45
 * @LastEditors: Vinton
 * @Description: In User Settings Edit
 * @FilePath: /dc-cocos-tools/dc_scene.js
 */
'use strict';
let { verificationProjectPath, getComponentFileName } = require('./utils/index')
/**
 * @description: 把业务的预制资源组件库,放到管理管理器去 ,把预制资源节点放到层级管理器去生成普通Node节点。
 * @param {*} path  资源管理器的文件夹名字。
 * @param {*} url 文件的路径
 * @param {*} uuid 当前选中的uuid
 * @param {*} AllComponent 是否全部组件都弄到项目里面去
 * @return {*}
 */
function getComponentForProject(path, url, uuid, AllComponent = false) {
    // 把组件库的组件放到我们的cocos项目当中去
    if(!url.length){
        Editor.error('当前组件不在组件库里面，请点击更新组件库')
        return
    }
    let callBack = function () {
        try {
            if (!url) return
            Editor.assetdb.import(url, `db://assets/${path}`, true, function (err, results) {
                results.forEach(function (result) {
                    if (result.type === 'prefab') {
                        // 插入个预制节点
                        if (!AllComponent) {
                            Editor.log('打印加载的预制资源', result.uuid, uuid)
                            Editor.Ipc.sendToPanel("scene", "scene:create-nodes-by-uuids", [result.uuid], uuid, { unlinkPrefab: null })
                        }
                    }
                });
            });
        } catch (error) {
            Editor.log(error)
        }
    }
    return callBack
}

let eventFuncs = {
    'set-AllComponents': function (event) {
        // Editor.log('set-AllComponents======>', getComponentFileName('Components'))
        verificationProjectPath(['Components'], getComponentForProject('Components', getComponentFileName('Components'), '', true))
    },
    'set-CountDown': function (event) {
        // Editor.log('set-CountDown======>', getComponentFileName('Components/Countdown'))
        let uuid = Editor.Selection.curSelection('node');
        verificationProjectPath(['Components'], getComponentForProject('Components', getComponentFileName('Components/Countdown'), uuid, false))
    },
    'set-Sound': function (event) {
        // Editor.log('set-Sound======>', getComponentFileName('Components/Sound'))
        // 获取当前选中的节点
        let uuid = Editor.Selection.curSelection('node');
        verificationProjectPath(['Components'], getComponentForProject('Components', getComponentFileName('Components/Sound'), uuid, false))
    },
    'set-loadScene': function (event) {
        // 获取当前选中的节点
        let uuid = Editor.Selection.curSelection('node');
        verificationProjectPath(['Components'], getComponentForProject('Components', getComponentFileName('Components/loadSceneProgress'), uuid, false))
    },
    'set-GameOver': function (event) {
        // 获取当前选中的节点
        let uuid = Editor.Selection.curSelection('node');
        verificationProjectPath(['Components'], getComponentForProject('Components', getComponentFileName('Components/GameOver'), uuid, false))
    },
    'HorizontalScreenControl': function () {
        let uuid = Editor.Selection.curSelection('node');
        verificationProjectPath(['Components'], getComponentForProject('Components', getComponentFileName('Components/landscapeMode'), uuid, false))
    },

    'create-core': function (event) {
        verificationProjectPath(['Script'], getComponentForProject('Script', getComponentFileName('Script/core'), '', true))
    },
    'create-utils': function (event) {
        verificationProjectPath(['Script'], getComponentForProject('Script', getComponentFileName('Script/utils'), '', true))
    },
    'create-Folders': function () {
        
        verificationProjectPath(['Components', 'Animation', 'Sound', 'Scene', 'Prefab', 'font', 'resources'], () => { }, false)
    },
    'set-active': function (event) {
        let nodes = Editor.Selection.curSelection('node');
        if (nodes && nodes.length != 0) {
            let active = cc.engine.getInstanceById(nodes[0]).active;
            for (let i = 0; i < nodes.length; i++) {
                const id = nodes[i];
                let node = cc.engine.getInstanceById(id)
                if (node) {
                    node.active = !active;
                }
            }
        }
    },
    'set-text': function () {
        Editor.log('测试=======》')
    }
}
module.exports = eventFuncs;